$(function(){
    //var scoringBox =$('.start-box');
    $('.start-box').each(function(){
    		$(this).on('click', '.icon-star', function(){
    			var rateItems = $(this).parent().find('.icon-star');
    			var skuId = $(this).parent().attr("data");
    			var l = rateItems.length;
    			// console.log($(this).index());
    			var stars = 0;
    			var currentIndex = $(this).index()+1;
    			rateItems.removeClass('selected-star');
    			for(var i = 0; i < currentIndex; i++){
    				if($(rateItems[i]).hasClass('selected-star')){
    					$(rateItems[i]).removeClass('selected-star');
    				}else{
    					$(rateItems[i]).addClass('selected-star');
    					stars++;
    				}
    			}
    			$("#starItem"+skuId).attr("stars",stars);
    			console.log("当前评论星数为"+stars)
    		});
    		
    })
    
    /*字数限制*/  
    $(".comments").each(function(){
    	$(this).on("input propertychange", function() {  
    		var $this = $(this);
    		var skuId = $this.attr("data");
    		_val = $this.val(),  
    		count = "";  
    		if (_val.length > 140) {  
    			$this.val(_val.substring(0, 140));  
    		}  
    		count = $this.val().length;  
    		$(this).next().find(".text-current-count").text(count); 
    		$("#starItem"+skuId).attr("comment",_val);
    		console.log("评论内容为"+_val)
    	}); 
    })
    
    $(".sub_btn").on("click",function(){
    	var items = $(".start-item");
    	var len = items.length;
    	var content = "";
    	var flg = false;
    	$(".start-item").each(function(i){
    		var orderId = $(this).attr("orderId");
    		var skuId = $(this).attr("skuId");
    		var stars = $(this).attr("stars");
    		if(stars == 0){
    			alert("请对商家服务进行打分");
    			flg = true;
    			return false;
    		}
    		var comment = $(this).attr("comment")
    		if(comment == null || comment == ""){
    			alert("请填写评论内容");
    			flg = true;
    			return false;
    		}
    		if(comment.length < 10){
    			alert("评论内容须大于十个字")
    			flg = true;
    			return false;
    		}
    		if(i == 0){
    			content = orderId+"#"+skuId+"#"+stars+"#"+comment;
    		}else{
    			content += ","+orderId+"#"+skuId+"#"+stars+"#"+comment;
    		}
    	})
    	if(flg){
    		return false;
    	}
    	console.log("要提交的串是"+content);
    	var url = "/order/comment";
    	$.ajax({
            type: 'GET',
            url: url,
            dataType: 'json',
            data:{
            	"comments":content,
            	"uid":$("#uid").val()
            },
            success: function(resp){
    			if(resp.code == 0){
    				alert("评论成功");
    			}else{
    				alert(resp.msg);
    			}
            },
            error: function(xhr, type){
                alert('Ajax error!');
            }
    	});
    })
})
